package com.um.jdy.common.activity.service.impl;

import cn.hutool.json.JSONUtil;
import com.um.jdy.common.activity.entity.vo.DiscountsAndStationVO;
import com.um.jdy.common.activity.entity.vo.DiscountsVO;
import com.um.jdy.common.activity.enums.ActivityEnum;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.jdy.common.activity.service.DiscountsStationService;
import com.um.jdy.common.activity.dao.DiscountsStationDao;
import com.um.jdy.common.activity.entity.po.DiscountsStation;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类：满减活动站场关联表
 */
@Service
@Transactional
@Slf4j
public class DiscountsStationServiceImpl extends BaseEntityServiceImpl<DiscountsStationDao,DiscountsStation>  implements DiscountsStationService{

    /**
     * 站场是否有满减金额活动
     *
     * @param deviceStationId
     * @return
     */
    @Override
    public boolean hasMoney(String deviceStationId, Integer amount) {
        return getAllDiscounts().stream().anyMatch(discounts ->
                ActivityEnum.DiscountsType.Money.name().equals(discounts.getType()) &&
                        (ActivityEnum.UseRange.All.name().equals(discounts.getAvailableStation()) ||
                                discounts.getDeviceStationIdList().contains(deviceStationId)) &&
                        discounts.getFullPrice() >= amount);
    }

    /**
     * 站场是否有满减打折活动活动
     *
     * @param deviceStationId
     * @return
     */
    @Override
    public boolean hasDiscount(String deviceStationId, Integer amount) {
        return getAllDiscounts().stream().anyMatch(discounts ->
                ActivityEnum.DiscountsType.Discount.name().equals(discounts.getType()) &&
                        (ActivityEnum.UseRange.All.name().equals(discounts.getAvailableStation()) ||
                                discounts.getDeviceStationIdList().contains(deviceStationId)) &&
                        discounts.getFullPrice() >= amount);
    }

    /**
     * 查询所有满减
     * @return
     */
    @Override
    public List<DiscountsAndStationVO> getAllDiscounts() {
        List<DiscountsAndStationVO> list = dao.getViewModelList("get_all_discounts_and_station_id", DiscountsAndStationVO.class, new String[]{getComId()});
        return list;
    }

    /**
     * 查询所有满减
     *
     * @param deviceStationId
     * @param deviceType
     * @return
     */
    @Override
    public List<DiscountsVO> getDiscountsList(String deviceStationId, String deviceType) {
        List<DiscountsVO> list = dao.getViewModelList("get_discounts_list", DiscountsVO.class,
                new String[]{deviceStationId});
        list = list.stream().filter(item ->
                ActivityEnum.UseRange.All.name().equals(item.getAvailableDevice()) ||
                        JSONUtil.toList(item.getDeviceTypes(), String.class).indexOf(deviceType) > -1)
                .collect(Collectors.toList());
        return list;

    }
}
